<?php

//引入自动加载文件
require 'vendor/autoload.php';

/**
 * 目前已经有：HTTP操作、多线程、模拟登陆等QueryList扩展
 * 下面来利用QueryList扩展来组合上面的例子，实现多线程采集文章并保存文章图片到本地
 */
use QL\QueryList;

// 永不过期
set_time_limit(0);

//HTTP操作扩展
$urls = QueryList::run('Request',[
    'target' => 'http://list.jd.com/list.html?cat=1713,3258,3297',
    'referrer'=>'http://list.jd.com',
    'method' => 'GET',
    'params' => ['var1' => 'testvalue', 'var2' => 'somevalue'],
    'user_agent'=>'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:21.0) Gecko/20100101 Firefox/21.0',
    'cookiePath' => './cookie.txt',
    'timeout' =>'60'
])->setQuery(['link' => ['div.gl-i-wrap div.p-name>a','href','',function($content){
        return 'http:'.$content;
//    }]],'#plist .gl-warp li:lt(40)')->getData(function($item){
    }]],'#plist .gl-warp li')->getData(function($item){
    return $item['link'];
});

//print_r($urls);exit;
//多线程扩展

QueryList::run('Multi',[
    'list' => $urls,
    'curl' => [
        'opt' => array(
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_AUTOREFERER => true,
        ),
        //设置线程数
        'maxThread' => 100,
        //设置最大尝试数
        'maxTry' => 3
    ],
    'success' => function($a){
        //采集规则
        $reg = array(
            //采集文章标题
            'title' => array('#name h1','text'),
            'title1' => array('#parameter2 li:eq(1)','text')

        );
        $rang = 'body';
        $ql = QueryList::Query($a['content'],$reg, $rang, 'GBK');
        $data = $ql->getData();
        //打印结果，实际操作中这里应该做入数据库操作
        print_r($data);
    }
]);